package com.summer.peony.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.summer.peony.system.mapper.SysDeptMapper;
import com.summer.peony.system.pojo.SysDept;
import com.summer.peony.system.pojo.SysDeptExample;
import com.summer.peony.system.pojo.SysDeptExample.Criteria;
import com.summer.peony.system.service.SysDeptService;

/**
 * @ClassName: DeptServiceImpl
 * @Description: TODO(部门管理业务逻辑实现层)
 * @author: TaoGe
 * @date: 2020年6月9日 下午3:49:22
 * @version: v0.0.1
 */
@Transactional
@Service
public class SysDeptServiceImpl implements SysDeptService {

	@Autowired
	private SysDeptMapper sysDeptMapper;

	@Override
	public Integer insert(SysDept sysDept) {
		// TODO 新增部门
		String code = sysDept.getCode();
		Date date = new Date();
		sysDept.setCreateTime(date);
		sysDept.setUpdateTime(date);
		sysDept.setDelFlag("0");
		sysDept.setDeptId(null);
		SysDeptExample example = new SysDeptExample();
		Criteria criteria = example.createCriteria();
		criteria.andCodeEqualTo(code);
		List<SysDept> sysDeptsList = sysDeptMapper.selectByExample(example);
		// 判断部门编码是否存在
		if (sysDeptsList.size() >= 1) {
			return 0;
		}
		return sysDeptMapper.insert(sysDept);
	}

	@Override
	public Integer deleteByPrimaryKey(Integer deptId) {
		// TODO 逻辑删除部门
		SysDept sysDept = sysDeptMapper.selectByPrimaryKey(deptId);
		sysDept.setDelFlag("1");
		SysDeptExample example = new SysDeptExample();
		Criteria criteria = example.createCriteria();
		criteria.andDelFlagEqualTo("0").andParentIdEqualTo(deptId);
		List<SysDept> list = sysDeptMapper.selectByExample(example);
		if (!list.isEmpty()) {
			return 2;
		}
		return sysDeptMapper.updateByPrimaryKey(sysDept);
	}

	@Override
	public Integer updateByPrimaryKey(SysDept record) {
		// TODO 修改部门信息
		SysDept sysDept = sysDeptMapper.selectByPrimaryKey(record.getDeptId());
		SysDeptExample example = new SysDeptExample();
		Criteria criteria = example.createCriteria();
		criteria.andCodeEqualTo(record.getCode()).andDeptIdNotEqualTo(record.getDeptId()).andDelFlagEqualTo("0");
		List<SysDept> list = sysDeptMapper.selectByExample(example);
		if (list.size() > 0) {
			return 0;
		}
		Date date = new Date();
		sysDept.setCode(record.getCode());
		sysDept.setName(record.getName());
		sysDept.setOrderNum(record.getOrderNum());
		sysDept.setUpdateTime(date);
		sysDept.setParentId(record.getParentId());
		return sysDeptMapper.updateByPrimaryKey(sysDept);
	}

	@Override
	public List<SysDept> selectAllDepts() {
		// TODO 查询全部部门列表
		SysDeptExample example = new SysDeptExample();
		Criteria criteria = example.createCriteria();
		// 不查询状态1：已删除
		criteria.andDelFlagEqualTo("0");
		// 将分页信息传给分页插件
		List<SysDept> list = sysDeptMapper.selectByExample(example);
		return list;

	}

	@Override
	public SysDept selectByPrimaryKey(Integer deptId) {
		// TODO 根据id查询部门
		return sysDeptMapper.selectByPrimaryKey(deptId);
	}

	@Override
	public PageInfo<SysDept> selectByNameDept(String name, String code, int pageNum, int pageSize) {
		// TODO 根据部门名称分页查询
		SysDeptExample example = new SysDeptExample();
		Criteria criteria = example.createCriteria();
		criteria.andDelFlagEqualTo("0").andNameLike("%" + name + "%").andCodeLike("%" + code + "%");
		PageHelper.startPage(pageNum, pageSize);
		List<SysDept> list = sysDeptMapper.selectByExample(example);
		PageInfo<SysDept> pageInfo = new PageInfo<SysDept>(list);
		return pageInfo;
	}

	@Override
	public List<SysDept> allSysDepts() {
		// 查询所有部门名称
		SysDeptExample example = new SysDeptExample();
		Criteria criteria = example.createCriteria();
		criteria.andDelFlagEqualTo("0");
		List<SysDept> list = sysDeptMapper.selectByExample(example);
		return getChildPerms(list, 0);
	}

	/**
	 * 根据父节点的ID获取所有子节点
	 * 
	 * @param list     分类表
	 * @param parentId 传入的父节点ID
	 * @return String
	 */
	public List<SysDept> getChildPerms(List<SysDept> list, int parentId) {
		List<SysDept> returnList = new ArrayList<SysDept>();
		for (Iterator<SysDept> iterator = list.iterator(); iterator.hasNext();) {
			SysDept t = (SysDept) iterator.next();
			// 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
			if (t.getParentId() == parentId) {
				recursionFn(list, t);
				returnList.add(t);
			}
		}
		return returnList;
	}

	/**
	 * 递归列表
	 * 
	 * @param list
	 * @param t
	 */
	private void recursionFn(List<SysDept> list, SysDept t) {
		// 得到子节点列表
		List<SysDept> childList = getChildList(list, t);
		t.setChildren(childList);
		for (SysDept tChild : childList) {
			if (hasChild(list, tChild)) {
				// 判断是否有子节点
				Iterator<SysDept> it = childList.iterator();
				while (it.hasNext()) {
					SysDept n = (SysDept) it.next();
					recursionFn(list, n);
				}
			}
		}
	}

	/**
	 * 得到子节点列表
	 */
	private List<SysDept> getChildList(List<SysDept> list, SysDept t) {
		List<SysDept> tlist = new ArrayList<SysDept>();
		Iterator<SysDept> it = list.iterator();
		while (it.hasNext()) {
			SysDept n = (SysDept) it.next();
			if (n.getParentId().longValue() == t.getDeptId().longValue()) {
				tlist.add(n);
			}
		}
		return tlist;
	}

	/**
	 * 判断是否有子节点
	 */
	private boolean hasChild(List<SysDept> list, SysDept t) {
		return getChildList(list, t).size() > 0 ? true : false;
	}
}
